<template>
<div>
  <el-divider content-position="center">添加产品</el-divider>
  <div class="add-product-from">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="产品名称：" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="生产日期：" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="产品数量：" prop="delivery">
        <el-input-number v-model="ruleForm.num" controls-position="right" @change="handleChange" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item label="价格：" prop="delivery">
        <el-input-number v-model="ruleForm.price" :precision="2" :step="0.1" ></el-input-number>
      </el-form-item>
      <el-form-item label="保质期：">
        <el-input-number v-model="ruleForm.fdate" :min="1" :step="1" ></el-input-number>天
      </el-form-item>
      <el-form-item label="产品照片：" prop="type">
        <el-upload
            class="upload-demo"
            action="http://localhost:8081/index/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="handleAvatarSuccess"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="种类：" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio value="1" label="1">素食</el-radio>
          <el-radio value="2" label="2">饮品</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="产品介绍：" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: "AddProduct",
  data() {
    return {
      ruleForm: {
        name: '',
        date: '',
        num: 0,
        price: 0.00,
        fdate:0,
        type: '',
        desc: ''
      },
      fileList:[],
      uploadList:[],
      rules: {
        name: [
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        date: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择产品种类', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请填写产品描述', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios({
            method:'post',
            url:'http://localhost:8081/boss/addFood',
            data:{
              name: this.ruleForm.name,
              date: this.ruleForm.date,
              num: this.ruleForm.num,
              price: this.ruleForm.price,
              fdate: this.ruleForm.fdate,
              type: this.ruleForm.type,
              desc: this.ruleForm.desc,
              uploadList:this.uploadList
            }
          }).then(res=>{
            if (res.data.code === 0){
              this.$message({
                message: '创建成功',
                type: 'success'
              });
            }
          }).catch(err=>{
            console.log(err)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleChange(value) {
      console.log(value);
    },
    handleRemove(file, fileList) {
      const index = this.uploadList.findIndex(item => item === file.response.data)
      if (index !== -1){
        this.uploadList.splice(index,1)
      }
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    handleAvatarSuccess(res, file) {
      this.uploadList.push(res.data)
      this.imageUrl = URL.createObjectURL(file.raw);
    },
  }
}
</script>

<style scoped>
.add-product-from{
  width: 40%;
  text-align: left;
  margin: 0 auto;
}
</style>
